******************************************************************************
* Purpose: Tests to assess conditional random assignment
******************************************************************************


do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

********************************************************************************
* Figure 1: Predicting Group Assignment of Mothers
********************************************************************************

use "$work\final_data1", clear
egen  timemuni=group(mfr_muni mfr_yob)
egen  timemuni1=group(fe mfr_yob)

eststo clear
eststo: areg peer_treat $covariates score_e1, absorb(time fe) vce(cluster group) 
*test conditional on FE
 testparm mfr_cs mfr_female mfr_lowbw mfr_preterm mfr_homebirth inc_m inc_f married cohab grund_m grund_f higher_m higher_f uni_m uni_f dk_m dk_f alder_m alder_f inp1y_pre out1y_pre gpmw_cons1y_pre gpmw_psyk1y_pre
 local test=round(r(p), 0.01)
 local F=round(r(F), 0.01)
coefplot, drop(_cons) msymbol(S) mcolor(gs6) mfcolor(gs6) ciopts(color(gs6)) xline(0, lcolor(gs10)) graphregion(color(white)) bgcolor(white) xscale(line noextend nofextend lcolor(gs10) lwidth(medthick)) yscale(line noextend nofextend lcolor(gs10) lwidth(medthick)) xlab(-0.2(0.1)0.2, labcolor(black) glcolor(gs10%33) grid angle(horizontal) tlcolor(gs10) tlwidth(medthick) ticks) ylab(,labcolor(black) glcolor(gs10%33) angle(horizontal) tlcolor(gs10) tlwidth(medthick) ticks)  text(0 0 "F-stat: `F' (p-value: `test')")
graph export "$out\randomassignment1.png", replace
esttab using "$out\randomassignment1.tex", fragment ///
b(3) se(3) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace
********************************************************************************
* Figure C4: Predicting Mother's own Screening Score
********************************************************************************
clear all
use "$work\final_data1", clear
label var score_e1_mean "Av. EPDS score in group"

eststo clear
eststo: areg score_e1 score_e1_mean mfr_first $covariates , absorb(fe time) vce(cluster group)
testparm  score_e1_mean  

coefplot, drop(_cons) msymbol(S) mcolor(gs6) mfcolor(gs6) ciopts(color(gs6)) xline(0, lcolor(gs10)) graphregion(color(white)) bgcolor(white) xscale(line noextend nofextend lcolor(gs10) lwidth(medthick)) yscale(line noextend nofextend lcolor(gs10) lwidth(medthick)) xlab(-3(1)3, labcolor(black) glcolor(gs10%33) grid angle(horizontal) tlcolor(gs10) tlwidth(medthick) ticks) ylab(,labcolor(black) glcolor(gs10%33) angle(horizontal) tlcolor(gs10) tlwidth(medthick) ticks)  text(0 0 /*"F-stat: 1.36 (0.119)" */)
graph export "$out\predict_ownEPDS.png", replace
esttab using "$out\predict_ownEPDS.tex", fragment ///
b(3) se(3) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace

********************************************************************************
* Covariates as outcome one at a time
********************************************************************************
use "$work\final_data1", clear


********************************************************************************
* Table C6
********************************************************************************
eststo clear
foreach v in $kidcovars {
eststo: areg `v' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
*parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\random`v'", replace)
estimates store k`v'
summarize `v' if peer_treat == 0, meanonly
estadd scalar means = r(mean)

}

esttab k* using "$out\random_assignment_v2kids.tex", fragment b(3) se(3) keep(peer_treat) stats(means N, fmt(%13.2fc %13.0fc) label("Control group mean" "Obs.")) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace

********************************************************************************
* Table C7
********************************************************************************
eststo clear
foreach v in $parentcovars1 {
eststo: areg `v' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
*parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\random`v'", replace)
estimates store p`v'
summarize `v' if peer_treat == 0, meanonly
estadd scalar means = r(mean)

}

esttab p* using "$out\random_assignment_v2parents1.tex", fragment b(3) se(3) keep(peer_treat) stats(means N, fmt(%13.2fc %13.0fc) label("Control group mean" "Obs.")) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace

********************************************************************************
* Table C8
********************************************************************************

eststo clear
foreach v in $parentcovars2 {
eststo: areg `v' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
*parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\random`v'", replace)
estimates store pp`v'
summarize `v' if peer_treat == 0, meanonly
estadd scalar means = r(mean)

}

esttab pp* using "$out\random_assignment_v2parents2.tex", fragment b(3) se(3) keep(peer_treat) stats(means N, fmt(%13.2fc %13.0fc) label("Control group mean" "Obs.")) se star(* 0.10 ** 0.05 *** 0.01) alignment(c c) label nolegend nonote replace

********************************************************************************
* Figure C5: Pre Trend Assessment of Main Outcomes
********************************************************************************
use "$work\final_data1", clear

eststo clear
forvalue i = 1/12 {
eststo: areg any`i'_pre peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\any`i'_pre", replace)
}
eststo clear
forvalue i = 1/12 {
eststo: areg gpmw_psyk`i'_pre peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\psyk`i'_pre", replace)
}
eststo clear
forvalue i = 1/12 {
eststo: areg gpmw_cons`i'_pre peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\cons`i'_pre", replace)
}
forvalue i = 1/12 {
eststo: areg adm`i'_pre peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\adm`i'_pre", replace)
}
use "$work\any1_pre",clear
gen m = 12
forvalue v = 2/12 {
append using "$work\any`v'_pre"
replace m = 13 - `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gswpre legend(off) xtitle("Months prior to birth") ytitle("Difference") ylab(-0.01(0.005)0.01, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\any_pre.png", replace

use "$work\psyk1_pre",clear
gen m = 12
forvalue v = 2/12 {
append using "$work\psyk`v'_pre"
replace m = 13 - `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gswpre legend(off) xtitle("Months prior to birth") ytitle("Difference") ylab(-0.01(0.005)0.01, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\psyk_pre.png", replace

use "$work\adm1_pre",clear
gen m = 12
forvalue v = 2/12 {
append using "$work\adm`v'_pre"
replace m = 13 - `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gswpre legend(off) xtitle("Months prior to birth") ytitle("Difference") ylab(-0.01(0.005)0.01, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\adm_pre.png", replace

use "$work\cons1_pre",clear
gen m = 12
forvalue v = 2/12 {
append using "$work\cons`v'_pre"
replace m = 13 - `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gswpre legend(off) xtitle("Months prior to birth") ytitle("Difference") ylab(-0.01(0.005)0.01, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\cons_pre.png", replace
